package com.sh4.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sh4.entity.WxUser;
import com.sh4.service.RoleService;
import com.sh4.service.impl.CoreServiceImpl;
import com.sh4.util.Sh4Utils;
import com.sh4.util.wechatUtils.Core;
import com.sh4.wxservice.ClientService;
import com.sh4.wxservice.User;

import net.sf.json.JSONObject;

@RestController
@RequestMapping(value = "role")
public class RoleController {
	
	@Autowired
	RoleService roleService;
	
	@Autowired
	CoreServiceImpl coreServiceImpl;
	
	@RequestMapping(value = "all",method = RequestMethod.POST)
	public Map<String, Object> selectRolesAll(@RequestBody JSONObject params,@RequestParam int pageNo,@RequestParam int pageSize) {
		Map<String, Object> result = new HashMap<String,Object>();
	
		int status = 200;
		String msg = "请求成功";
		List<Map<String, Object>> list = this.roleService.selectedRolesAll(params,pageNo,pageSize);
		if (list == null) {
			status = 404;
			msg = "请求失败";
		}else {
			result.put("dataList", list);
		}
		result.put("status", status);
		result.put("msg", msg);
		return result;
	}
	
	@RequestMapping(value = "loginAll",method = RequestMethod.POST)
	public Map<String, Object> loginAll(@RequestBody JSONObject params) {
		
		Map<String, Object> result = new HashMap<String,Object>();
		int status = 200;
		String msg = "请求成功";
		try {
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			
			//测试服务号推消息 
//			ClientService.MSText("oY_PQ0j2RVBIdO1FF9TYghGSTpP4", "登录页面有人访问了。好开心！"+sdf.format(new Date()), Sh4Utils.getAccess_token());
//			ClientService.MSText("oY_PQ0qGJ7ZHJl_mMwaT0MlN4IRI", "登录页面有人访问了。好开心！"+sdf.format(new Date()), Sh4Utils.getAccess_token());
			
			//刷新服务号用户信息
//			JSONObject res =  Core.getUserList(Sh4Utils.getAccess_token(), "");
//			System.out.println(res.toString());
//			JSONObject data = res.getJSONObject("data");
//			List<String> openIds = data.getJSONArray("openid");
//			for(String openId : openIds) {
//				//根据openid 获取用户信息保存
//				WxUser user = User.userInfo(openId, "zh_CN", Sh4Utils.getAccess_token());
//				coreServiceImpl.save(user);
//			}
			
			List<Map<String, Object>> list = this.roleService.loginAll();
			result.put("dataList", list);
		} catch (Exception e) {
			status = 300;
			msg = "内部错误";
		}
		
		result.put("status", status);
		result.put("msg", msg);
		return result;
	}
	
	@RequestMapping(value = "add",method = RequestMethod.POST)
	public Map<String, Object> addRole(@RequestBody JSONObject params) {
		Map<String, Object> result = new HashMap<String,Object>();
		System.out.println(params);
		
		int state = this.roleService.addRoleByJsonObject(params);
		
		int status = 200;
		String msg = "请求成功！";
		
		if (state == 0) {
			status = 404;
			msg = "角色添加失败";
		}
		result.put("status", status);
		result.put("msg", msg);
		return result;
	}

	@RequestMapping(value = "delete",method = RequestMethod.POST)
	public Map<String, Object> deleteRole(@RequestBody JSONObject params) {
		Map<String, Object> result = new HashMap<String,Object>();
		int status = 200;
		String msg = "请求成功！";
		int state = this.roleService.deleteRoleAndRolePermission(params);
		if (state == 0) {
			status = 404;
			msg = "删除角色失败";	
		}
		result.put("status", status);
		result.put("msg", msg);
		return result;
	}

	@RequestMapping(value = "search", method = RequestMethod.POST)
	public Map<String, Object> searchRoles(@RequestBody JSONObject params) {
		System.out.println("***********8"+params);
		Map<String, Object> result = new HashMap<String,Object>();
		int status = 200;
		String msg = "请求成功！";
		
		List<Map<String, Object>>  roles = roleService.searchRolesBy(params);
		
		result.put("status", status);
		result.put("msg", msg);
		result.put("dataList", roles);
		return result;
	}
	

	public Map<String, Object> name() {
		Map<String, Object> result = new HashMap<String,Object>();
		int status = 200;
		String msg = "请求成功！";
		
		result.put("status", status);
		result.put("msg", msg);
		return result;
	}
	
	

}
